package flc.ast.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.os.Bundle;
import android.os.Environment;
import android.view.View;

import com.blankj.utilcode.util.FileUtils;
import com.blankj.utilcode.util.PathUtils;
import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.adapter.FileAdapter;
import flc.ast.databinding.ActivityLocalFileBinding;
import flc.ast.utils.Content;
import flc.ast.utils.VideoUtils;
import stark.common.basic.event.EventStatProxy;
import stark.common.basic.utils.FileUtil;
import stark.common.basic.utils.MediaUtil;

public class LocalFileActivity extends BaseAc<ActivityLocalFileBinding> {

    private String mCurrentRootPath;
    private FileAdapter fileAdapter;
    private List<String> mList = new ArrayList<>();

    @Override
    protected int onCreate() {
        return R.layout.activity_local_file;
    }

    @Override
    public void onBackPressed() {
        EventStatProxy.getInstance().statEvent1(this, mDataBinding.event1Container);

        if (mCurrentRootPath.equals(Environment.getExternalStorageDirectory().getPath())) {
            super.onBackPressed();
        } else {
            mCurrentRootPath = FileUtils.getDirName(mCurrentRootPath);
            mCurrentRootPath = mCurrentRootPath.substring(0, mCurrentRootPath.length() - 1);
            getData(mCurrentRootPath);
        }
    }

    @Override
    protected void initView() {
        mDataBinding.ivLocalFileBack.setOnClickListener(v -> onBackPressed());

        mDataBinding.ivUseFolder.setOnClickListener(this);

        mDataBinding.rvFileList.setLayoutManager(new LinearLayoutManager(mContext));
        fileAdapter = new FileAdapter();
        mDataBinding.rvFileList.setAdapter(fileAdapter);
        fileAdapter.setOnItemClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.ivUseFolder://使用文件夹
                File sourceFile = new File(mCurrentRootPath);
                File destinationFolder = new File(PathUtils.getFilesPathExternalFirst() + Content.myFolder);
                // 如果目标文件夹不存在，则创建它
                if (!destinationFolder.exists()) {
                    destinationFolder.mkdirs();
                }
                // 构造目标文件路径
                File destinationFile = new File(destinationFolder.getPath() + File.separator + sourceFile.getName());
                FileUtils.copy(sourceFile, destinationFile);
                ToastUtils.showShort(getString(R.string.add_suc));
                break;
        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        String path = fileAdapter.getItem(position);
        if (FileUtils.isDir(path)) {
            mCurrentRootPath = path;
            getData(mCurrentRootPath);
        } else {
            PlayActivity.videoPath = path;
            startActivity(PlayActivity.class);
        }
    }

    @Override
    protected void initData() {
        mCurrentRootPath = Environment.getExternalStorageDirectory().getPath();
        getData(mCurrentRootPath);

    }

    private void getData(String rootPath) {
        if (rootPath.equals(Environment.getExternalStorageDirectory().getPath())) {
            mDataBinding.tvLocalFileTitle.setText(getString(R.string.local_file));
            mDataBinding.ivUseFolder.setImageResource(R.drawable.sywjw);
        } else {
            mDataBinding.tvLocalFileTitle.setText(FileUtils.getFileName(rootPath));
            mDataBinding.ivUseFolder.setImageResource(R.drawable.sywjj);
        }
        List<File> files = FileUtils.listFilesInDir(rootPath);

        mList.clear();
        for (File file : files) {
            if (FileUtils.isDir(file) || VideoUtils.isVideoFile(file.getPath())) {
                mList.add(file.getPath());
            }
        }
        if (mList.size() == 0) {
            mDataBinding.tvFileNoData.setVisibility(View.VISIBLE);
            mDataBinding.rvFileList.setVisibility(View.GONE);
        } else {
            mDataBinding.tvFileNoData.setVisibility(View.GONE);
            mDataBinding.rvFileList.setVisibility(View.VISIBLE);
            mDataBinding.rvFileList.scrollToPosition(0);
            fileAdapter.setList(mList);
        }
    }

}